﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Communication.Local.LRPC;
using UtilZ.Dotnet.Ex.Json;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Tool.WatchdogPlugin
{
    internal class HostQueryHelper
    {

        //private static ApiResult PrimitiveQuery(string urlSuffix)
        //{
        //    using var httpClient = new HttpClient();
        //    httpClient.Timeout = TimeSpan.FromSeconds(10);
        //    string url = $"http://127.0.0.1:{WatchdogConfig.Instance.HostQueryPort}/api/v1/os/{urlSuffix}";
        //    Task<string> task = httpClient.GetStringAsync(url);
        //    return JsonSerializer.DeserializeObject<ApiResult>(task.Result);
        //}

        public static string QueryProArgs(int pid)
        {
            try
            {
                //ApiResult result = PrimitiveQuery($"queryProArgs?pid={pid}");
                //if (result.Status != ApiResultStatus.Succes)
                //{
                //    Loger.Warn($"主机上查询进程参数失败,失败原因:{result.Data}");
                //    return null;
                //}

                //return result.Data;

               return (string)LRPCCore.RemoteCallF(QUERY_PRO_ARGS, pid);
            }
            catch (Exception ex)
            {
                Loger.Warn(ex, "主机上查询进程参数异常");
                return null;
            }
        }

        public static List<int> GetCrushWindowInfoList()
        {
            try
            {
                //ApiResult result = PrimitiveQuery("getCrushProcessIdList");
                //if (result.Status != ApiResultStatus.Succes)
                //{
                //    Loger.Warn($"主机上查询崩溃进程列表失败,{result.Data}");
                //    return null;
                //}

                //return JsonSerializer.DeserializeObject<List<int>>(result.Data);            

                return (List<int>)LRPCCore.RemoteCallF(GET_CRUSH_WINDOW_LIST, null);
            }
            catch (Exception ex)
            {
                Loger.Warn(ex, "主机上查询崩溃进程列表异常");
                return null;
            }
        }

        public static bool IsNoResponse(IntPtr mainWindowHandle)
        {
            try
            {
                //ApiResult result = PrimitiveQuery($"isNoResponse?handler={mainWindowHandle.ToInt64()}");
                //if (result.Status != ApiResultStatus.Succes)
                //{
                //    Loger.Warn($"主机上查检进程是否响应失败,{result.Data}");
                //    return false;
                //}

                //return bool.Parse(result.Data);

                return (bool)LRPCCore.RemoteCallF(IS_NO_RESONSE, mainWindowHandle);
            }
            catch (Exception ex)
            {
                Loger.Warn(ex, "主机上查检进程是否响应异常");
                return false;
            }
        }




        public const string QUERY_PRO_ARGS = "QUERY_PRO_ARGS";
        public const string GET_CRUSH_WINDOW_LIST = "GET_CRUSH_WINDOW_LIST";
        public const string IS_NO_RESONSE = "IS_NO_RESONSE";

      
    }
}
